// import axios from "axios";
import * as api from '../../../http/api'
import http from '../../../http/http'

const state={
  pageCount:0,
  postAddsData:[],
  postSearchsData:[],
  postDeletesData:[],
  postChangesData:[],
  postSupplysData:[],
  postPagingsData:[],
}
const actions={
  // 新增消耗品方法
  postAddData({commit},obj){
    console.log(obj)
    return http({
      url:api.consumablesManagementAdd, //记得修改接口
      method:"post",
      data:obj
    }).then(res=>{
      // 目的是将data存在vuex里面
      console.log(res.data)
      console.log(res)
      commit('storeAddData',res.data)//这里要注意data到底是什么 data
      return res.code
    })
  },
  // 查询消耗品方法
  postSearchData({commit},obj){
    console.log(obj)
    return http({
      url:api.consumablesManagementSelect, //记得修改接口
      method:'post',
      data:obj
    }).then(res=>{

      console.log(res.data)
      // commit('storeSearchData',res.data.conditionTypeSelectOfPage) //这里要注意data到底是什么 data
      commit('storeSearchData',res.data)

      return res.code
    })
  },
  // 删除消耗品
  postDeleteData({commit},obj){
    console.log(obj)
    return http({
      url:api.consumablesManagementDelete,// 修改接口
      method:'post',
      data:obj
    }).then(res=>{
      console.log(res)
      commit('storeDeleteData',res.data) //注意
      return res.code 
    })
  },
  // 修改消耗品
  postChangeData({commit},obj){
    console.log(obj)
    return http({
      url:api.consumablesManagementUpdate,
      method:'post',
      data:obj
    }).then(res=>{
      console.log(res)
      commit('storeChangeData',res.data)
      return res.code
    })
  },
  // 消耗品补货
  postSupplyData({commit},obj){
    console.log(obj)
    return http({
      url:api.consumablesManagementSupplement,
      method:'post',
      data:obj
    }).then(res=>{
      console.log(res)
      commit('storeSupplyData',res.data)
      return res.code
    })
  },
  // 消耗品分页
  postPagingData({commit},obj){
    return http({
      url:api.consumablesManagementPage,
      method:'post',
      data:obj
    }).then(res=>{
      console.log(res.data)
      // commit('storePagingData',res.data.selectOfPage)
      commit('storePagingData',res.data)
      return res.data
    })
  }
}
const mutations={
  storeAddData(state,obj){
    console.log(obj)
    state.postAddsData=obj
  },
  storeSearchData(state,obj){
    console.log(obj.conditionSelectOfPage)
    state.pageCount=obj.pageCount
    state.postSearchsData=obj.conditionSelectOfPage
    // console.log(obj.conditionSelectOfPage)
  },
  storeDeleteData(state,obj){
    state.postDeletesData=obj
  },
  storeChangeData(state,obj){
    state.postChangesData=obj
  },
  storeSupplyData(state,obj){
    state.postSupplysData=obj
  },
  storePagingData(state,obj){
    console.log()
    state.pageCount=obj.pageCount
    state.postPagingsData=obj.selectOfPage
  }
}
const getters={
  getPageCount(state){
    return state.pageCount
  },
  getstorePagingData(state){
    console.log(state.postPagingsData)

    state.postPagingsData.forEach(item=>{
      let mydelFlag = {}
      if(item.delFlag===0) {
        mydelFlag = {
          value: 0,
          label: '正常'
        }
      } else if(item.delFlag===1) {
        mydelFlag = {
          value: 1,
          label: '删除'
        }
      }
      item.delFlag = mydelFlag
    });
    console.log(state.postPagingsData)

    return state.postPagingsData
  },
  getstoreSupplyData(state){
    state.postSupplysData.forEach(item=>{
      let mydelFlag = {}
      if(item.delFlag===0) {
        mydelFlag = {
          value: 0,
          label: '正常'
        }
      } else if(item.delFlag===1) {
        mydelFlag = {
          value: 1,
          label: '删除'
        }
      }
      item.delFlag = mydelFlag
    });
    return state.postSupplysData
  },
  getstoreChangeData(state){
    state.postChangesData.forEach(item=>{
      let mydelFlag = {}
      if(item.delFlag===0) {
        mydelFlag = {
          value: 0,
          label: '正常'
        }
      } else if(item.delFlag===1) {
        mydelFlag = {
          value: 1,
          label: '删除'
        }
      }
      item.delFlag = mydelFlag
    });
    return state.postChangesData
  },
  getstoreDeleteData(state){
    state.postDeletesData.forEach(item=>{
      let mydelFlag = {}
      if(item.delFlag===0) {
        mydelFlag = {
          value: 0,
          label: '正常'
        }
      } else if(item.delFlag===1) {
        mydelFlag = {
          value: 1,
          label: '删除'
        }
      }
      item.delFlag = mydelFlag
    });
    return state.postDeletesData
  },
  getstoreSearchData(state){

    state.postSearchsData.forEach(item=>{
      let mydelFlag = {}
      if(item.delFlag===0) {
        mydelFlag = {
          value: 0,
          label: '正常'
        }
      } else if(item.delFlag===1) {
        mydelFlag = {
          value: 1,
          label: '删除'
        }
      }
      item.delFlag = mydelFlag
    });
    return state.postSearchsData
  },
  getstoreAddData(state){
    state.postAddsData.forEach(item=>{
      let mydelFlag = {}
      if(item.delFlag===0) {
        mydelFlag = {
          value: 0,
          label: '正常'
        }
      } else if(item.delFlag===1) {
        mydelFlag = {
          value: 1,
          label: '删除'
        }
      }
      item.delFlag = mydelFlag
    });
    return state.postAddsData
  },

}


export default{
  namespaced:true,
  state,
  actions,
  mutations,
  getters
}